/*
 * @Author: 贺楠 1071780426@qq.com
 * @Date: 2024-09-10 11:12:20
 * @LastEditors: 贺楠 1071780426@qq.com
 * @LastEditTime: 2024-09-11 09:57:41
 * @FilePath: \monitoring_system\src\components\testMachineManage\equipmentManage\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import publicForm from "@/components/public/el-forms.vue"; //弹框form
import userImage from "@/components/public/userImage-upload.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置项
import {
    updateMachineInfo, //保存
    getMachineInfo, //详情

} from "@/api/testMachineManage/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",
        "typ"
    ],
    data() {
        return {
            visible: "",
            loading: false,
            title: null,
            isLock: false,
            dialogHeight: null,
            treeData: [], //材料种类
            formData: {
                fId: "", //主键id
                companyId: "", //标段id
                companyName: "", //标段名称
                machineName: "", //设备名称
                machineType: "", //设备型号
                machineCode: "", //设备编码
                machineModel: "", //设备类型
                remoteTool: "", //远程工具
                remoteCode: "", //远程号
                remotePassword: "", //远程密码
                controllerManufacturer: "", //控制器厂家
                isImplementation: "", //是否实施
                isImplementations:1, //是否实施
                implementationDates: this.common.day(0), //实施日期
                registrationCode: "", //设备注册码
                authorizationDates: "", //授权有效期
                isUse: "", //是否启用
                isUses: 1, //是否启用
                userName: "", //实施人员
                remark: "", //备注
                deviceLatitude: "", //设备纬度
                deviceLongitude: "" //设备经度


            },
            formObj: formJs.formObj,
            formDatafileList: [], //工作计划资料
            btnShow: true,
            currentDetail: null,
            selected: [],
            typs: null,
            fileList: []
        };
    },
    components: {
        publicForm,
        userImage
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },
        typ: {
            handler(newVal) {
                if (newVal) {
                    this.typs = newVal;
                    if (newVal == 1) {
                        this.formObj[0].label = "生产厂家"
                    } else if (newVal == 2) {
                        this.formObj[0].label = "供应商"

                    }
                }
            },
            immediate: true,
        },
        statusNum: {
            handler(newVal) {
                if (newVal) {

                    this.edit(newVal);
                }
            },
            immediate: true,
        },
        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.currentDetail = newVal;
                    if (this.statusNum != 1) {
                        this.getDetail({
                            fId: this.currentDetail.fId
                        })
                    }
                }
            },
            immediate: true,
        },
    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：
    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("monitorBack-userInfo"));
        this.btnShow = true
     
       if (this.statusNum == 20) { //编辑
            this.formData.fId = this.currentDetail.fId //按钮 
            this.getDetail({
                fId: this.currentDetail.fId
            })
            this.formObj.map((item) => {
                item.disabled = false;
            });
            // this.formData.isImplementations=1; //是否实施
            // this.formData.isUses=1; //是否启动

            this.formObj[0].disabled=true;
            this.formObj[1].disabled=true;
            this.formObj[2].disabled=true;
            this.formObj[3].disabled=true;
            this.btnShow = true
        } else if (this.statusNum == 7) { //查看
            this.btnShow = false
            this.formObj.map((item) => {
                item.disabled = true;
            });
        }
        this.getHeight();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.5 + "px";
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },
        selectChange(data, a, b) {
            console.log(data, a, b);
        },
        edit() {
            this.formData = this.$options.data.call(this).formData;
        },

        upFileList(e) {
            this.fileList = e;
            console.log(this.fileList);
        },
        // 刪除文件
        delteFile(file) {
            for (let index = 0; index < this.fileList.length; index++) {
                if (file.uid == this.fileList[index].uid) {
                    if (this.fileList[index].atta == undefined) {
                        this.formData.recordfiles.push({
                            fId: this.fileList[index].fId,
                        });
                    } else {
                        this.formData.recordfiles.push({
                            fId: this.fileList[index].atta.fId,
                        });
                    }
                    this.fileList.splice(index, 1);
                }
            }
        },
        // 详情
        getDetail(obj) {
            this.loading = true;
            getMachineInfo(obj).then((res) => {
                if (res.errcode == 0) {
                    var data = res.data.yjQmpMachineInfo;
                    this.formData = data
                    this.loading = false;
                    // if(data.isUse=="是"){
                    //     this.formData.isUses=0;
                    // }else if(data.isUse=="否"){
                    //     this.formData.isUses=1;
                    // }
                    // if(data.isImplementation=="是"){
                    //     this.formData.isImplementations=0;
                    // }else if(data.isImplementation=="否"){
                    //     this.formData.isImplementations=1;
                    // }
                    if (this.statusNum == 7) {
                        this.btnShow = false;
                    } else {
                        this.btnShow = true;
                    }
                } else {
                    this.loading = false;
                    this.$message.error(res.msg);
                }
            });
        },
        yj_saveForm() {
            // this.formData.userId = this.userInfo.userId; //当前登录人id
          
         
            // 0连续新增1新增
            if (this.$refs.publicForm.submitForm()) {
                // if(this.formData.isUses==1){
                //     this.formData.isUse="是";
                // }else if(this.formData.isUses==0){
                //     this.formData.isUse="否";

                // }
                // if(this.formData.isImplementations==1){
                //     this.formData.isImplementation="是";
                // }else if(this.formData.isImplementations==0){
                //     this.formData.isImplementation="否";

                // }
                this.saveForm(this.formData);
            } else {
                console.log("error");
            }
        },
        // 保存
        saveForm(data) {
            this.loading = true;
       
            this.isLock = true
            updateMachineInfo(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.visible = false;
                        this.$emit("refreshList");
                        this.$message.success("新增成功");
                        this.isLock = false
                    }  else {
                        this.loading = false;
                        this.isLock = false
                        this.$message.error(res.data);
                    }
                })
        },
    },
};